﻿using System;
using System.Collections.Generic;
using System.Linq;
using Helpers;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.BarterSystem.Barterables;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.MapNotificationTypes;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.Election
{
	// Token: 0x02000274 RID: 628
	public class MakePeaceKingdomDecision : KingdomDecision
	{
		// Token: 0x06002143 RID: 8515 RVA: 0x0008D7EE File Offset: 0x0008B9EE
		internal static void AutoGeneratedStaticCollectObjectsMakePeaceKingdomDecision(object o, List<object> collectedObjects)
		{
			((MakePeaceKingdomDecision)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x06002144 RID: 8516 RVA: 0x0008D7FC File Offset: 0x0008B9FC
		protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			collectedObjects.Add(this.FactionToMakePeaceWith);
		}

		// Token: 0x06002145 RID: 8517 RVA: 0x0008D811 File Offset: 0x0008BA11
		internal static object AutoGeneratedGetMemberValueFactionToMakePeaceWith(object o)
		{
			return ((MakePeaceKingdomDecision)o).FactionToMakePeaceWith;
		}

		// Token: 0x06002146 RID: 8518 RVA: 0x0008D81E File Offset: 0x0008BA1E
		internal static object AutoGeneratedGetMemberValueDailyTributeToBePaid(object o)
		{
			return ((MakePeaceKingdomDecision)o).DailyTributeToBePaid;
		}

		// Token: 0x06002147 RID: 8519 RVA: 0x0008D830 File Offset: 0x0008BA30
		internal static object AutoGeneratedGetMemberValue_applyResults(object o)
		{
			return ((MakePeaceKingdomDecision)o)._applyResults;
		}

		// Token: 0x06002148 RID: 8520 RVA: 0x0008D842 File Offset: 0x0008BA42
		public MakePeaceKingdomDecision(Clan proposerClan, IFaction kingdomToMakePeaceWith, int dailyTributeToBePaid = 0, bool applyResults = true) : base(proposerClan)
		{
			this.FactionToMakePeaceWith = kingdomToMakePeaceWith;
			this.DailyTributeToBePaid = dailyTributeToBePaid;
			this._applyResults = applyResults;
		}

		// Token: 0x06002149 RID: 8521 RVA: 0x0008D864 File Offset: 0x0008BA64
		public override bool IsAllowed()
		{
			TextObject textObject;
			return Campaign.Current.Models.KingdomDecisionPermissionModel.IsPeaceDecisionAllowedBetweenKingdoms(base.Kingdom, this.FactionToMakePeaceWith as Kingdom, out textObject);
		}

		// Token: 0x0600214A RID: 8522 RVA: 0x0008D898 File Offset: 0x0008BA98
		public override int GetProposalInfluenceCost()
		{
			return Campaign.Current.Models.DiplomacyModel.GetInfluenceCostOfProposingPeace(base.ProposerClan);
		}

		// Token: 0x0600214B RID: 8523 RVA: 0x0008D8B4 File Offset: 0x0008BAB4
		public override TextObject GetGeneralTitle()
		{
			TextObject textObject = new TextObject("{=v3xdiFfD}Make Peace With {KINGDOM_NAME}", null);
			textObject.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.Name);
			return textObject;
		}

		// Token: 0x0600214C RID: 8524 RVA: 0x0008D8D8 File Offset: 0x0008BAD8
		public override TextObject GetSupportTitle()
		{
			TextObject textObject;
			if (this.DailyTributeToBePaid == 0)
			{
				textObject = new TextObject("{=0aXG8dvJ}Make peace with the {KINGDOM_NAME}. No tribute will be paid.", null);
			}
			else if (this.DailyTributeToBePaid > 0)
			{
				textObject = new TextObject("{=2b1xZGaQ}Make peace with the {KINGDOM_NAME}. Our kingdom will pay {T} tribute daily.", null);
			}
			else
			{
				textObject = new TextObject("{=NjPMRWbW}Make peace with the {KINGDOM_NAME}. Our kingdom will receive {T} tribute daily.", null);
			}
			textObject.SetTextVariable("T", MathF.Abs(this.DailyTributeToBePaid));
			textObject.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
			return textObject;
		}

		// Token: 0x0600214D RID: 8525 RVA: 0x0008D94D File Offset: 0x0008BB4D
		public override TextObject GetChooseTitle()
		{
			TextObject textObject = new TextObject("{=4GgfFDWG}Making peace with the {KINGDOM_NAME}", null);
			textObject.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
			return textObject;
		}

		// Token: 0x0600214E RID: 8526 RVA: 0x0008D974 File Offset: 0x0008BB74
		public override TextObject GetSupportDescription()
		{
			TextObject textObject = TextObject.Empty;
			if (this.DailyTributeToBePaid != 0)
			{
				textObject = new TextObject("{=BlVjIlZF}{FACTION_LEADER} will decide if the {KINGDOM_CONSIDERING_PEACE_NAME} will make peace with the {KINGDOM_NAME} by paying {TRIBUTE_PERCENTAGE} percent of the kingdom's income as tribute. You can pick your stance regarding this decision.", null);
				textObject.SetTextVariable("T", this.DailyTributeToBePaid);
			}
			else
			{
				textObject = new TextObject("{=awoeb3br}{FACTION_LEADER} will decide if the {KINGDOM_CONSIDERING_PEACE_NAME} will make peace with the {KINGDOM_NAME}. You can pick your stance regarding this decision.", null);
			}
			textObject.SetTextVariable("FACTION_LEADER", this.DetermineChooser().Leader.Name);
			textObject.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
			textObject.SetTextVariable("KINGDOM_CONSIDERING_PEACE_NAME", base.Kingdom.InformalName);
			return textObject;
		}

		// Token: 0x0600214F RID: 8527 RVA: 0x0008DA08 File Offset: 0x0008BC08
		public override TextObject GetChooseDescription()
		{
			TextObject textObject = TextObject.Empty;
			if (this.DailyTributeToBePaid != 0)
			{
				textObject = new TextObject("{=n4I3pWOn}As the ruler, you must decide if peace will be made with the {KINGDOM_NAME} by paying {TRIBUTE_PERCENTAGE} percent of the kingdom's income as tribute.", null);
				textObject.SetTextVariable("TRIBUTE_PERCENTAGE", this.DailyTributeToBePaid);
			}
			else
			{
				textObject = new TextObject("{=KFHj7ckm}As the ruler, you must decide if peace will be made with the {KINGDOM_NAME}", null);
			}
			textObject.SetTextVariable("IS_FEMALE", this.DetermineChooser().Leader.IsFemale ? 1 : 0);
			textObject.SetTextVariable("KINGDOM_NAME", this.FactionToMakePeaceWith.InformalName);
			return textObject;
		}

		// Token: 0x06002150 RID: 8528 RVA: 0x0008DA89 File Offset: 0x0008BC89
		public override IEnumerable<DecisionOutcome> DetermineInitialCandidates()
		{
			yield return new MakePeaceKingdomDecision.MakePeaceDecisionOutcome(true, base.Kingdom, this.FactionToMakePeaceWith);
			yield return new MakePeaceKingdomDecision.MakePeaceDecisionOutcome(false, base.Kingdom, this.FactionToMakePeaceWith);
			yield break;
		}

		// Token: 0x06002151 RID: 8529 RVA: 0x0008DA99 File Offset: 0x0008BC99
		public override Clan DetermineChooser()
		{
			return base.Kingdom.RulingClan;
		}

		// Token: 0x06002152 RID: 8530 RVA: 0x0008DAA6 File Offset: 0x0008BCA6
		protected override bool ShouldBeCancelledInternal()
		{
			return this.FactionToMakePeaceWith.IsEliminated || !base.Kingdom.IsAtWarWith(this.FactionToMakePeaceWith);
		}

		// Token: 0x06002153 RID: 8531 RVA: 0x0008DACC File Offset: 0x0008BCCC
		public override void DetermineSponsors(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
		{
			foreach (DecisionOutcome decisionOutcome in possibleOutcomes)
			{
				if (((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)decisionOutcome).ShouldPeaceBeDeclared)
				{
					decisionOutcome.SetSponsor(base.ProposerClan);
				}
				else
				{
					base.AssignDefaultSponsor(decisionOutcome);
				}
			}
		}

		// Token: 0x06002154 RID: 8532 RVA: 0x0008DB38 File Offset: 0x0008BD38
		public override void ApplyChosenOutcome(DecisionOutcome chosenOutcome)
		{
			if (this._applyResults && ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)chosenOutcome).ShouldPeaceBeDeclared)
			{
				MakePeaceAction.ApplyByKingdomDecision(base.Kingdom, this.FactionToMakePeaceWith, this.DailyTributeToBePaid);
			}
		}

		// Token: 0x06002155 RID: 8533 RVA: 0x0008DB68 File Offset: 0x0008BD68
		public override bool OnShowDecision()
		{
			if (this.FactionToMakePeaceWith == Clan.PlayerClan.Kingdom && !Hero.MainHero.Clan.IsUnderMercenaryService)
			{
				if (!Hero.MainHero.IsPrisoner)
				{
					TextObject textObject = new TextObject("{=1V8f9vRM}A courier bearing a peace offer from the {PROPOSER_HERO_FACTION} has arrived at the court of your realm.", null);
					textObject.SetTextVariable("PROPOSER_HERO_FACTION", base.ProposerClan.Leader.MapFaction.InformalName);
					Campaign.Current.CampaignInformationManager.NewMapNoticeAdded(new PeaceOfferMapNotification(base.ProposerClan.MapFaction, this.DailyTributeToBePaid, textObject));
				}
				return false;
			}
			return true;
		}

		// Token: 0x06002156 RID: 8534 RVA: 0x0008DBFA File Offset: 0x0008BDFA
		public override TextObject GetSecondaryEffects()
		{
			return new TextObject("{=!}All supporters gains some relation with each other.", null);
		}

		// Token: 0x06002157 RID: 8535 RVA: 0x0008DC07 File Offset: 0x0008BE07
		public override void ApplySecondaryEffects(MBReadOnlyList<DecisionOutcome> possibleOutcomes, DecisionOutcome chosenOutcome)
		{
			bool applyResults = this._applyResults;
		}

		// Token: 0x06002158 RID: 8536 RVA: 0x0008DC10 File Offset: 0x0008BE10
		public override TextObject GetChosenOutcomeText(DecisionOutcome chosenOutcome, KingdomDecision.SupportStatus supportStatus, bool isShortVersion = false)
		{
			TextObject textObject;
			if (((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)chosenOutcome).ShouldPeaceBeDeclared)
			{
				if (base.IsSingleClanDecision())
				{
					textObject = new TextObject("{=CswzBb02}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM}.", null);
				}
				else if (supportStatus == KingdomDecision.SupportStatus.Majority)
				{
					textObject = new TextObject("{=17A2DDgD}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM} with the support of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.", null);
				}
				else if (supportStatus == KingdomDecision.SupportStatus.Minority)
				{
					textObject = new TextObject("{=JDfnPFsW}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM} despite the opposition of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.", null);
				}
				else
				{
					textObject = new TextObject("{=aEt1kqxm}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided to make peace with the {KINGDOM}, with his {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council evenly divided on the matter.", null);
				}
			}
			else if (base.IsSingleClanDecision())
			{
				textObject = new TextObject("{=wsDNxArW}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has chosen not to make peace with the {KINGDOM}.", null);
			}
			else if (supportStatus == KingdomDecision.SupportStatus.Majority)
			{
				textObject = new TextObject("{=mRrYn2qm}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has chosen not to make peace with the {KINGDOM} with the support of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.", null);
			}
			else if (supportStatus == KingdomDecision.SupportStatus.Minority)
			{
				textObject = new TextObject("{=Ing5gFbO}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has chosen not to make peace with the {KINGDOM} over the objections of {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council.", null);
			}
			else
			{
				textObject = new TextObject("{=AThZtg7U}{PEACEMAKER_RULER.NAME} of the {KINGDOM_CONSIDERING_PEACE} has decided against making peace with the {KINGDOM}, with his {?PEACEMAKER_RULER.GENDER}her{?}his{\\?} council evenly divided on the matter.", null);
			}
			StringHelpers.SetCharacterProperties("PEACEMAKER_RULER", base.Kingdom.Leader.CharacterObject, textObject, false);
			textObject.SetTextVariable("KINGDOM_CONSIDERING_PEACE", base.Kingdom.InformalName);
			textObject.SetTextVariable("KINGDOM", this.FactionToMakePeaceWith.InformalName);
			return textObject;
		}

		// Token: 0x06002159 RID: 8537 RVA: 0x0008DD04 File Offset: 0x0008BF04
		public override DecisionOutcome GetQueriedDecisionOutcome(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
		{
			return possibleOutcomes.FirstOrDefault((DecisionOutcome t) => ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)t).ShouldPeaceBeDeclared);
		}

		// Token: 0x0600215A RID: 8538 RVA: 0x0008DD2B File Offset: 0x0008BF2B
		public float CalculateSupport(Clan clan)
		{
			return this.DetermineSupport(clan, new MakePeaceKingdomDecision.MakePeaceDecisionOutcome(true, base.Kingdom, this.FactionToMakePeaceWith));
		}

		// Token: 0x0600215B RID: 8539 RVA: 0x0008DD48 File Offset: 0x0008BF48
		public override float DetermineSupport(Clan clan, DecisionOutcome possibleOutcome)
		{
			MakePeaceKingdomDecision.MakePeaceDecisionOutcome makePeaceDecisionOutcome = (MakePeaceKingdomDecision.MakePeaceDecisionOutcome)possibleOutcome;
			float valueForFaction = (float)new PeaceBarterable(base.Kingdom, this.FactionToMakePeaceWith, CampaignTime.Years(1f)).GetValueForFaction(clan);
			float num = (clan.Leader != null) ? ((clan.Leader.Gold < 50000) ? (1f + 0.5f * ((50000f - (float)clan.Leader.Gold) / 50000f)) : ((clan.Leader.Gold > 200000) ? MathF.Max(0.66f, MathF.Pow(200000f / (float)clan.Leader.Gold, 0.4f)) : 1f)) : 1f;
			int traitLevel = clan.Leader.GetTraitLevel(DefaultTraits.Generosity);
			float num2 = (this.DailyTributeToBePaid > 0) ? (1f - 0.1f * (float)MathF.Max(-2, MathF.Min(2, traitLevel))) : 1f;
			float num3 = (valueForFaction - (float)((int)((float)Campaign.Current.Models.DiplomacyModel.GetValueOfDailyTribute(this.DailyTributeToBePaid) * num * num2))) * Campaign.Current.Models.DiplomacyModel.DenarsToInfluence();
			if (makePeaceDecisionOutcome.ShouldPeaceBeDeclared)
			{
				float num4 = num3;
				int num5 = clan.Leader.GetTraitLevel(DefaultTraits.Mercy) * 10;
				return num4 + (float)num5;
			}
			float num6 = -num3;
			int num7 = -clan.Leader.GetTraitLevel(DefaultTraits.Mercy) * 10;
			return num6 + (float)num7;
		}

		// Token: 0x04000A57 RID: 2647
		[SaveableField(101)]
		public readonly IFaction FactionToMakePeaceWith;

		// Token: 0x04000A58 RID: 2648
		[SaveableField(103)]
		private readonly bool _applyResults;

		// Token: 0x04000A59 RID: 2649
		[SaveableField(110)]
		public readonly int DailyTributeToBePaid;

		// Token: 0x02000589 RID: 1417
		public class MakePeaceDecisionOutcome : DecisionOutcome
		{
			// Token: 0x060045EF RID: 17903 RVA: 0x0014AF1C File Offset: 0x0014911C
			internal static void AutoGeneratedStaticCollectObjectsMakePeaceDecisionOutcome(object o, List<object> collectedObjects)
			{
				((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
			}

			// Token: 0x060045F0 RID: 17904 RVA: 0x0014AF2A File Offset: 0x0014912A
			protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
			{
				base.AutoGeneratedInstanceCollectObjects(collectedObjects);
				collectedObjects.Add(this.Kingdom);
				collectedObjects.Add(this.FactionToMakePeaceWith);
			}

			// Token: 0x060045F1 RID: 17905 RVA: 0x0014AF4B File Offset: 0x0014914B
			internal static object AutoGeneratedGetMemberValueShouldPeaceBeDeclared(object o)
			{
				return ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)o).ShouldPeaceBeDeclared;
			}

			// Token: 0x060045F2 RID: 17906 RVA: 0x0014AF5D File Offset: 0x0014915D
			internal static object AutoGeneratedGetMemberValueKingdom(object o)
			{
				return ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)o).Kingdom;
			}

			// Token: 0x060045F3 RID: 17907 RVA: 0x0014AF6A File Offset: 0x0014916A
			internal static object AutoGeneratedGetMemberValueFactionToMakePeaceWith(object o)
			{
				return ((MakePeaceKingdomDecision.MakePeaceDecisionOutcome)o).FactionToMakePeaceWith;
			}

			// Token: 0x060045F4 RID: 17908 RVA: 0x0014AF77 File Offset: 0x00149177
			public MakePeaceDecisionOutcome(bool shouldPeaceBeDeclared, Kingdom kingdom, IFaction factionToMakePeaceWith)
			{
				this.ShouldPeaceBeDeclared = shouldPeaceBeDeclared;
				this.Kingdom = kingdom;
				this.FactionToMakePeaceWith = factionToMakePeaceWith;
			}

			// Token: 0x060045F5 RID: 17909 RVA: 0x0014AF94 File Offset: 0x00149194
			public override TextObject GetDecisionTitle()
			{
				TextObject textObject = new TextObject("{=kakxnaN5}{?SUPPORT}Yes{?}No{\\?}", null);
				textObject.SetTextVariable("SUPPORT", this.ShouldPeaceBeDeclared ? 1 : 0);
				return textObject;
			}

			// Token: 0x060045F6 RID: 17910 RVA: 0x0014AFBC File Offset: 0x001491BC
			public override TextObject GetDecisionDescription()
			{
				if (base.SponsorClan != null && this.Kingdom != null && this.FactionToMakePeaceWith != null && base.SponsorClan != Clan.PlayerClan)
				{
					TextObject empty = TextObject.Empty;
					if (this.ShouldPeaceBeDeclared)
					{
						Campaign.Current.Models.DiplomacyModel.GetScoreOfDeclaringPeace(this.Kingdom, this.FactionToMakePeaceWith, base.SponsorClan, out empty);
					}
					if (empty != TextObject.Empty)
					{
						return empty;
					}
				}
				if (this.ShouldPeaceBeDeclared)
				{
					return new TextObject("{=THz06NQD}It is time to make peace", null);
				}
				return new TextObject("{=jQpeuHIE}We oppose making peace at this time", null);
			}

			// Token: 0x060045F7 RID: 17911 RVA: 0x0014B04E File Offset: 0x0014924E
			public override string GetDecisionLink()
			{
				return null;
			}

			// Token: 0x060045F8 RID: 17912 RVA: 0x0014B051 File Offset: 0x00149251
			public override ImageIdentifier GetDecisionImageIdentifier()
			{
				return null;
			}

			// Token: 0x0400171F RID: 5919
			[SaveableField(100)]
			public readonly bool ShouldPeaceBeDeclared;

			// Token: 0x04001720 RID: 5920
			[SaveableField(110)]
			public readonly Kingdom Kingdom;

			// Token: 0x04001721 RID: 5921
			[SaveableField(120)]
			public readonly IFaction FactionToMakePeaceWith;
		}
	}
}
